package dao.daoImpl;

import common.DataSourceUtils;
import dao.LoginMapper;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ColumnListHandler;
import pojo.Function;
import pojo.User;

import java.sql.SQLException;
import java.util.List;

/**
 * Created by Administrator on 2017-11-20.
 */
public class LoginMapperImpl implements LoginMapper {


    public User selectUserByNamePwd(String username, String password) throws SQLException {
        String sql="select * from user where username=? and password=?";
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        User user=qr.query(sql,new BeanHandler<>(User.class),username,password);
        return user;


    }

    @Override
    public  List<Function> getFunction(int roleId,String functionDoc) throws SQLException {
        String sql="select * from function  where moduleId=? and functionDoc=? order by functionId";
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        List<Function> functionList= qr.query(sql,new BeanListHandler<>(Function.class),roleId,functionDoc);

        return functionList;
    }

    @Override
    public List<Function> getFunctionDoc() throws SQLException {
        String sql="select * from function ";
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
        List<Function> functionDocs= qr.query(sql,new BeanListHandler<Function>(Function.class));
        return functionDocs;
    }

    @Override
    public Function checkUrl(int roleId, String urls) throws SQLException {
        String sql="select count(*) from function where moduleId=? and url=? and functionDoc=?";
        QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());

        return qr.query(sql, new BeanHandler<Function>(Function.class),roleId,urls,"打开");
    }
}
